const mongoose = require('mongoose');
const Dept = require('../models/Dept');
const Permission = require('../models/Permission');
const Role = require('../models/Role');
const User = require('../models/User');
require('dotenv').config();

const MONGO_URI = process.env.MONGO_URI;

async function initData() {
    try {
        await mongoose.connect(MONGO_URI);

        // 清空现有数据
        console.log("1----------");
        console.log(Dept);
        await Dept.deleteMany({});
        await Permission.deleteMany({});
        console.log("2----------");
        console.log(Role);
        await Role.deleteMany({});
        await User.deleteMany({});

        // 创建部门
        const sunDept = await Dept.create({ name: '太阳升起', isDisplay: true, status: '正常' });
        const dept1 = await Dept.create({ name: '技术部', parentId: sunDept._id, isDisplay: true, status: '正常' });
        const dept2 = await Dept.create({ name: '市场部', parentId: sunDept._id, isDisplay: true, status: '正常' });

        // 创建权限 (先创建父权限，再使用其 _id 作为子权限的 parentId)
        const deptManagePermission = await Permission.create({ name: '部门管理', code: 'dept:manage', type: '菜单' });
        const permissionManagePermission = await Permission.create({ name: '权限管理', code: 'permission:manage', type: '菜单' });
        const roleManagePermission = await Permission.create({ name: '角色管理', code: 'role:manage', type: '菜单' });
        const userManagePermission = await Permission.create({ name: '用户管理', code: 'user:manage', type: '菜单' });

        const permissions = await Permission.insertMany([
            { name: '部门新增', code: 'dept:create', type: '按钮', parentId: deptManagePermission._id },
            { name: '部门修改', code: 'dept:update', type: '按钮', parentId: deptManagePermission._id },
            { name: '部门删除', code: 'dept:delete', type: '按钮', parentId: deptManagePermission._id },
            { name: '部门查询', code: 'dept:query', type: '按钮', parentId: deptManagePermission._id },
            { name: '权限新增', code: 'permission:create', type: '按钮', parentId: permissionManagePermission._id },
            { name: '权限修改', code: 'permission:update', type: '按钮', parentId: permissionManagePermission._id },
            { name: '权限删除', code: 'permission:delete', type: '按钮', parentId: permissionManagePermission._id },
            { name: '权限查询', code: 'permission:query', type: '按钮', parentId: permissionManagePermission._id },
            { name: '角色新增', code: 'role:create', type: '按钮', parentId: roleManagePermission._id },
            { name: '角色修改', code: 'role:update', type: '按钮', parentId: roleManagePermission._id },
            { name: '角色删除', code: 'role:delete', type: '按钮', parentId: roleManagePermission._id },
            { name: '角色查询', code: 'role:query', type: '按钮', parentId: roleManagePermission._id },
            { name: '用户新增', code: 'user:create', type: '按钮', parentId: userManagePermission._id },
            { name: '用户修改', code: 'user:update', type: '按钮', parentId: userManagePermission._id },
            { name: '用户删除', code: 'user:delete', type: '按钮', parentId: userManagePermission._id },
            { name: '用户查询', code: 'user:query', type: '按钮', parentId: userManagePermission._id }
        ]);

        // 创建角色
        const adminRole = await Role.create({
            name: '管理员',
            code: 'admin',
            status: '正常',
            permissions: permissions.map(p => p._id)
        });

        const queryPermissions = permissions.filter(p => p.code.endsWith(':query')).map(p => p._id);
        const userRole = await Role.create({
            name: '普通用户',
            code: 'user',
            status: '正常',
            permissions: queryPermissions
        });

        // 创建用户
        const adminUser = await User.create({
            userName: 'admin',
            password: '123456',
            nickName: '超级管理员',
            roles: [adminRole._id]  // 将管理员角色 ID 添加到 roles 数组
        });

        const user1 = await User.create({
            userName: 'user1',
            password: '123456',
            nickName: '普通用户1',
            roles: [userRole._id]  // 将普通用户角色 ID 添加到 roles 数组
        });

        const user2 = await User.create({
            userName: 'user2',
            password: '123456',
            nickName: '普通用户2',
            roles: [userRole._id]  // 将普通用户角色 ID 添加到 roles 数组 
        });

        console.log('初始化数据成功');
    } catch (error) {
        console.error('初始化数据失败:', error);
    } finally {
        await mongoose.disconnect();
    }
}

initData();